package com.viroyal.monitorlib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.suntiago.baseui.utils.log.Slog;
import com.viroyal.monitorlib.service.MainStartService;
import com.viroyal.monitorlib.utils.Constants;
import com.viroyal.monitorlib.utils.SPUtils;

/**
 * Created by cxy_nj on 2017/2/9.
 */

public class AirPlaneModeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isOpen = intent.getBooleanExtra("state", false);
        boolean isStarted = SPUtils.getInstance(context).get(Constants.IS_SERVICE_STARTED, false);
        Slog.e("AirPlaneModeReceiver", "isopen:" + isOpen + ",isstarted:" + isStarted);
        if (!isOpen && !isStarted) {
            context.startService(new Intent(context, MainStartService.class));
            SPUtils.getInstance(context).put(Constants.IS_SERVICE_STARTED, true);
        } else if (isOpen && isStarted) {
            context.stopService(new Intent(context, MainStartService.class));
            SPUtils.getInstance(context).put(Constants.IS_SERVICE_STARTED, false);
        }
    }
}
